//
//  GuidPage.swift
//  Uber
//
//  Created by chen on 16/3/10.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit
import AVFoundation

class GuidPage: FxBasePage {

    @IBOutlet var backImageView: UIImageView!
    
    
    var player:AVPlayer!
    var playerItem:AVPlayerItem!
    
    //初始化location
    var location: FxLocation!
    override func viewDidLoad() {
        super.viewDidLoad()
        initPlayVideo()
        //动画效果
        doAnimation()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func doAnimation(){
        var images:[UIImage] = []
        var image:UIImage?
        var imageName:String?
        for index in 0...67{
            imageName = "logo-" + String(format: "%03d", index)
            image = UIImage(named: imageName!)
            images.insert(image!, atIndex: index)
        }
        backImageView.animationImages = images
        backImageView.animationRepeatCount = 1
        backImageView.animationDuration = 5
        
        backImageView.startAnimating()
        
        //播放动画效果
        UIView.animateWithDuration(0.7, delay: 5, options: .CurveEaseOut, animations: { (finish) -> Void in
            print("Animation Start")
            //显示我们的backview容器
            self.backView.alpha = 1.0
            //播放我们的视频
            self.player.play()
            }) { (finish) -> Void in
                print("Animation End")
        }
        
    }
    func initPlayVideo(){
        //获取资源文件
        let path = NSBundle.mainBundle().pathForResource("welcome_video", ofType: "mp4")
        let url = NSURL.fileURLWithPath(path!)
        //初始化playerItem
        self.playerItem = AVPlayerItem(URL: url)
        //初始化player
        self.player = AVPlayer(playerItem:self.playerItem)
        //播放视频需要一个layer承载
        //加入layer层来放置播放图层
        //错误发生在这个地方：let playerLayer = AVPlayerLayer(layer: player)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = UIScreen.mainScreen().bounds
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
        //0表示不覆盖我们的登陆，注册按钮
        backView.layer.insertSublayer(playerLayer, atIndex: 0)
        backView.alpha = 0.0
        
        //为了能够循环的播放video，需要加入通知的方式,object是参数
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "didFinishVideo:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
       
    }
    func didFinishVideo(sender:NSNotification){
        let item = sender.object as! AVPlayerItem
        //Moves the playback cursor to a given time
        item.seekToTime(kCMTimeZero)
        self.player.play()
    }
    @IBAction func loginButton(sender: AnyObject) {
//        location = FxLocation()
//        location.startLocation()
//        self.performSelector("showHomePage", withObject: self, afterDelay: 1.0)
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.showHomePage()
        
    }

    @IBAction func registerButton(sender: AnyObject) {
        let page = RegisterPage()
        let navPage = UINavigationController(rootViewController: page)
        self.presentViewController(navPage, animated: true, completion: nil)
    }
}
